/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Runner.java
 *
 * Created on 19-gen-2010, 14.31.06
 */
package ch.epfl.dslab.conferrng.wizard;

import ch.epfl.dslab.conferrng.engine.PluginFactory;
import ch.epfl.dslab.conferrng.engine.RunnerPlugin;
import ch.epfl.dslab.conferrng.gui.ParameterCellEditor;
import ch.epfl.dslab.conferrng.gui.Utils;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.TableColumn;
import org.netbeans.spi.wizard.Wizard;
import org.netbeans.spi.wizard.WizardPage;
import org.netbeans.spi.wizard.WizardPanelNavResult;

/**
 *
 * @author lokeller
 */
public class Runner extends WizardPage {

    private List listOfAvailableRunners;
    public static final String PROP_LISTOFAVAILABLERUNNERS = "listOfAvailableRunners";

    /**
     * Get the value of listOfAvailableRunners
     *
     * @return the value of listOfAvailableRunners
     */
    public List getListOfAvailableRunners() {
        return listOfAvailableRunners;
    }

    /**
     * Set the value of listOfAvailableRunners
     *
     * @param listOfAvailableRunners new value of listOfAvailableRunners
     */
    public void setListOfAvailableRunners(List listOfAvailableRunners) {
        List oldListOfAvailableRunners = this.listOfAvailableRunners;
        this.listOfAvailableRunners = listOfAvailableRunners;
        firePropertyChange(PROP_LISTOFAVAILABLERUNNERS, oldListOfAvailableRunners, listOfAvailableRunners);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        systemRunBean = new ch.epfl.dslab.conferrng.wizard.SystemRunBean();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        runnerParamsTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        experimentOutcomeText = new javax.swing.JTextField();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${listOfAvailableRunners}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jComboBox1);
        bindingGroup.addBinding(jComboBoxBinding);
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${injectionParent.faultInjectionPlan.runnerPlugin.class.name}"), jComboBox1, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Select the runner plugin");

        runnerParamsTable.setModel(new  javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Name", "Type", "Value"
            }));

            eLProperty = org.jdesktop.beansbinding.ELProperty.create("${injectionParent.faultInjectionPlan.runnerPlugin.parameters}");
            org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, runnerParamsTable);
            org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${name}"));
            columnBinding.setColumnName("Name");
            columnBinding.setColumnClass(String.class);
            columnBinding.setEditable(false);
            columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${type}"));
            columnBinding.setColumnName("Type");
            columnBinding.setColumnClass(String.class);
            columnBinding.setEditable(false);
            columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${value}"));
            columnBinding.setColumnName("Value");
            columnBinding.setColumnClass(String.class);
            bindingGroup.addBinding(jTableBinding);
            jTableBinding.bind();
            runnerParamsTable.addFocusListener(new java.awt.event.FocusAdapter() {
                public void focusLost(java.awt.event.FocusEvent evt) {
                    runnerParamsTableFocusLost(evt);
                }
            });
            runnerParamsTable.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    runnerParamsTablePropertyChange(evt);
                }
            });
            jScrollPane1.setViewportView(runnerParamsTable);
            runnerParamsTable.getColumnModel().getColumn(0).setResizable(false);
            runnerParamsTable.getColumnModel().getColumn(1).setResizable(false);
            runnerParamsTable.getColumnModel().getColumn(2).setResizable(false);
            runnerParamsTable.getColumnModel().getColumn(2).setCellEditor(new ParameterCellEditor(injectionParent.getFaultInjectionPlan()));

            jLabel2.setText("Runner parameters");

            jLabel3.setText("Experiments outcome folder");

            jButton1.setText("Browse");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });

            binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${injectionParent.faultInjectionEngine.outputDir}"), experimentOutcomeText, org.jdesktop.beansbinding.BeanProperty.create("text"));
            bindingGroup.addBinding(binding);

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jComboBox1, 0, 587, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 587, Short.MAX_VALUE)
                        .addComponent(jLabel2)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(experimentOutcomeText, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(14, 14, 14)
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(jButton1)
                        .addComponent(experimentOutcomeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(34, 34, 34))
            );

            bindingGroup.bind();
        }// </editor-fold>//GEN-END:initComponents
    private Map<String, RunnerPlugin> map;
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        String s = (String) jComboBox1.getSelectedItem();
        if (s != null) {
            if (!map.containsKey(s)) {
                try {
                    RunnerPlugin plugin = (RunnerPlugin) PluginFactory.newInstance(s, getInjectionParent().getFaultInjectionPlan());
                    map.put(s, plugin);
                } catch (Exception ex) {
                    Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            getInjectionParent().getFaultInjectionPlan().setRunnerPlugin(map.get(s));
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void runnerParamsTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_runnerParamsTablePropertyChange
        if (evt.getPropertyName().equals("model")) {
            TableColumn col = runnerParamsTable.getColumnModel().getColumn(2);
            col.setCellEditor(new ParameterCellEditor(getInjectionParent().getFaultInjectionPlan()));
        }
    }//GEN-LAST:event_runnerParamsTablePropertyChange

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String selectedValue = Utils.loadDirectory(this, getInjectionParent().getFaultInjectionPlan(), "Select output directory");
        if (selectedValue != null) {
            getInjectionParent().getFaultInjectionEngine().setOutputDir(selectedValue);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void runnerParamsTableFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_runnerParamsTableFocusLost
        
    }//GEN-LAST:event_runnerParamsTableFocusLost

    private InjectionWizardController injectionParent;
    public static final String PROP_INJECTIONPARENT = "injectionParent";

    /**
     * Get the value of injectionParent
     *
     * @return the value of injectionParent
     */
    public InjectionWizardController getInjectionParent() {
        return injectionParent;
    }

    /**
     * Set the value of injectionParent
     *
     * @param injectionParent new value of injectionParent
     */
    public void setInjectionParent(InjectionWizardController injectionParent) {
        map = new HashMap<String, RunnerPlugin>();
        InjectionWizardController oldInjectionParent = this.injectionParent;
        this.injectionParent = injectionParent;
        initComponents();
        setListOfAvailableRunners(getInjectionParent().getClassesForInterface(RunnerPlugin.class));

        firePropertyChange(PROP_INJECTIONPARENT, oldInjectionParent, injectionParent);
    }

    @Override
    public WizardPanelNavResult allowNext(String stepName, Map settings, Wizard wizard) {

        /* stop editing if table is being edited */
        if ( runnerParamsTable.getCellEditor() != null) {
            runnerParamsTable.getCellEditor().stopCellEditing();
        }
        return super.allowNext(stepName, settings, wizard);
    }



    public static String getDescription() {
        return "System under test";
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField experimentOutcomeText;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable runnerParamsTable;
    private ch.epfl.dslab.conferrng.wizard.SystemRunBean systemRunBean;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
